
package cnhy.salary.controller;

import cn.hutool.core.date.DateUtil;
import com.easy.query.api4j.sql.SQLWherePredicate;
import com.easy.query.core.expression.lambda.SQLExpression1;
import io.swagger.annotations.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import lombok.*;
import com.ruoyi.common.core.page.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import cnhy.salary.domain.OutputGroup;
//import cnhy.salary.service.OutputGroupService;
import ling.common.*;
import ling.common.domain.*;

 /** 产线产出记录 控制层
 * @author : He-LiangLiang@Outlook.com
 * @date : 2025-5-26
 */
@Api(tags = "产线产出记录 功能接口")
@RestController
@RequestMapping("/salary/OutputGroup")
public class OutputGroupController extends EQueryController<OutputGroup>{
    

    
    @ApiOperation("详情")
    @PreAuthorize("@ps.any(@pay.outputGroup.query)")
    @GetMapping(value = "/{id}")
    public Result<OutputGroup> getInfo(@PathVariable("id") Long id)
    {
       return super.getInfo(id);
    }
    
    @ApiOperation("分页列表")
    @PreAuthorize("@ps.any(@pay.outputGroup.list)")
    @GetMapping("list")
    public ResultRows<OutputGroup> list(OutputGroup dto, PageDomain pageRequest) {

        SQLExpression1<SQLWherePredicate<OutputGroup>> exp = null;
        if (dto.getTime() != null) {
            var time = DateUtil.beginOfDay(dto.getTime());
            var endTime = DateUtil.endOfDay(dto.getTime());
            exp = p -> p.rangeClosed(OutputGroup::getTime, time, endTime);
        }
        return queryPage(dto, pageRequest, exp);
    }
    
    @ApiOperation("新增")
    @PreAuthorize("@ps.any(@pay.outputGroup.add)")
    @Log(title = "产线产出记录", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@Validated @RequestBody OutputGroup entity) {
        var old = queryHelper.entityQuery(OutputGroup.class)
                .where(o -> o.sourceId().eq(entity.getSourceId()))
                .where(p -> p.time().rangeClosed(DateUtil.beginOfDay(entity.getTime()), DateUtil.endOfDay(entity.getTime())))
                .firstOrNull();
        if (old != null)
            return Result.bad("产线日产出已存在");
        entity.setType(1);
        entity.setStatus("1");
        return super.add(entity);
    }
    
    @ApiOperation("删除")
    @PreAuthorize("@ps.any(@pay.outputGroup.remove)")
    @Log(title = "产线产出记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable("ids") Long[] ids){
        return super.remove(Arrays.stream(ids).toArray());
    }
    
    @ApiOperation("编辑")
    @PreAuthorize("@ps.any(@pay.outputGroup.edit)")
    @Log(title = "产线产出记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public  Result edit(@Validated @RequestBody OutputGroup entity){
        return super.edit(entity);
    }
    
    @ApiOperation("导出")
    @PreAuthorize("@ps.any(@pay.outputGroup.export)")
    @Log(title = "产线产出记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OutputGroup entity, PageDomain pageRequest,HttpServletResponse response)
    {
        super.export(entity,pageRequest,null,response);
    }
}